package com.iams.manage.service.impl;

import java.util.List;
import java.util.stream.Collectors;

import com.iams.activiti8.service.impl.WorkFlowStarterImpl;
import com.iams.common.utils.DateUtils;
import com.iams.common.utils.uuid.UUID;
import com.iams.manage.domain.PendingEvaluation;
import com.iams.manage.domain.workflow.dto.IdentificationRecordDTO;
import com.iams.manage.mapper.PendingEvaluationMapper;
import com.iams.manage.service.IPendingEvaluationService;
import com.iams.manage.service.impl.converter.IdentificationRecordConverterImpl;
import com.sun.jna.platform.win32.Winspool;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.iams.manage.mapper.IdentificationRecordMapper;
import com.iams.manage.domain.workflow.IdentificationRecord;
import com.iams.manage.service.IIdentificationRecordService;

/**
 * 鉴定申请记录Service业务层处理
 *
 * @author LiuTao
 * @date 2025-04-11
 */
@Service
public class IdentificationRecordServiceImpl implements IIdentificationRecordService
{
    @Autowired
    private IdentificationRecordMapper identificationRecordMapper;


    @Autowired
    private IdentificationRecordConverterImpl identificationRecordConverter;

    @Autowired
    private WorkFlowStarterImpl workFlowStarter;

    @Autowired
    private PendingEvaluationMapper pendingEvaluationMapper;

    /**
     * 查询鉴定申请记录
     *
     * @param id 鉴定申请记录主键
     * @return 鉴定申请记录
     */
    @Override
    public IdentificationRecord selectIdentificationRecordById(Long id)
    {
        return identificationRecordMapper.selectIdentificationRecordById(id);
    }

    /**
     * 查询鉴定申请记录列表
     *
     * @param identificationRecord 鉴定申请记录
     * @return 鉴定申请记录
     */
    @Override
    public List<IdentificationRecordDTO> selectIdentificationRecordList(IdentificationRecord identificationRecord)
    {
        List<IdentificationRecord> list = identificationRecordMapper.selectIdentificationRecordList(identificationRecord);

        return list.stream()
                .map(identificationRecordEntity -> identificationRecordConverter.entityToDto(identificationRecordEntity))
                .collect(Collectors.toList());
    }

    /**
     * 新增鉴定申请记录
     *
     * @param identificationRecordDTO 鉴定申请记录
     * @return 结果
     */
    @Override
    public int insertIdentificationRecord(IdentificationRecordDTO identificationRecordDTO)
    {
        List<IdentificationRecord> identificationRecordList = identificationRecordMapper.selectIdentificationRecordList(new IdentificationRecord(identificationRecordDTO.getUserId(), identificationRecordDTO.getArchiveId()));
        for(IdentificationRecord identificationRecordEntity : identificationRecordList)
        {
            // 这里的逻辑是检查是否有已批准的鉴定记录
            // 实际应该去检查档案状态，后续追加验证注解
            if(identificationRecordEntity.getStatus().equals("已批准")) {
                throw new RuntimeException("该档案已被鉴定，请勿重复鉴定");
            }
            else if(identificationRecordEntity.getStatus().contains("审批"))
            {
                throw new RuntimeException("请勿重复发起申请");
            }
        }
        IdentificationRecord identificationRecord = identificationRecordConverter.dtoToEntity(identificationRecordDTO);
        identificationRecord.setBusinessKey(UUID.randomUUID().toString());

        String instanceId = workFlowStarter.startProcessInstanceByDefinitionKey(identificationRecord.getProcessName(), identificationRecord.getBusinessKey(), identificationRecordDTO.getUserName()+"鉴定申请");

        identificationRecord.setInstanceId(instanceId);
        identificationRecord.setStartApplyTime(DateUtils.getNowDate());
        identificationRecord.setStatus("待审批");


        // 获取待审批的表单，修改状态
        PendingEvaluation pendingEvaluation = pendingEvaluationMapper.selectPendingEvaluationByArchiveId(identificationRecordDTO.getArchiveId());
        pendingEvaluation.setStatus("已启动审批流程");
        pendingEvaluationMapper.updatePendingEvaluation(pendingEvaluation);


        return identificationRecordMapper.insertIdentificationRecord(identificationRecord);
    }

    /**
     * 修改鉴定申请记录
     *
     * @param identificationRecord 鉴定申请记录
     * @return 结果
     */
    @Override
    public void updateIdentificationRecord(IdentificationRecord identificationRecord)
    {
        identificationRecordMapper.updateIdentificationRecord(identificationRecord);
    }

    /**
     * 修改鉴定申请记录
     *
     * @param identificationRecordDTO 鉴定申请记录
     * @return 结果
     */
    @Override
    public int updateIdentificationRecord(IdentificationRecordDTO identificationRecordDTO)
    {
        IdentificationRecord identificationRecord = identificationRecordConverter.dtoToEntity(identificationRecordDTO);
        return identificationRecordMapper.updateIdentificationRecord(identificationRecord);
    }


    /**
     * 批量删除鉴定申请记录
     *
     * @param ids 需要删除的鉴定申请记录主键
     * @return 结果
     */
    @Override
    public int deleteIdentificationRecordByIds(Long[] ids)
    {
        for (Long id : ids)
            deleteIdentificationRecordById(id);
        return 1;
    }

    /**
     * 删除鉴定申请记录信息
     *
     * @param id 鉴定申请记录主键
     * @return 结果
     */
    @Override
    public int deleteIdentificationRecordById(Long id)
    {
        workFlowStarter.deleteProcessInstance(identificationRecordMapper.selectIdentificationRecordById(id).getInstanceId());
        return identificationRecordMapper.deleteIdentificationRecordById(id);
    }
}
